﻿using UnityEngine;
using System.Collections;
using Memo.Electricity.Engine;
using UnityEngine.Assertions.Must;
using System;

namespace Memo.Electricity.Engine
{
	public class CSceneFadeEffect : CPostEffectsBase
	{
		public class WaitForSceneFade : CWaitable
		{
			public bool isDown = false;
			public WaitForSceneFade()
				=> ready = () =>
				{
					return isDown;
				};
		}

		private const string sEffectPath = "Fx/SceneFadeEffect";
		public static WaitForSceneFade PlayerSceneFadeEffect(Color maskColor, bool isForward)
		{
			Camera target = Camera.main;
			if (target.transform.GetComponent<CSceneFadeEffect>())
			{
				Destroy(target.transform.GetComponent<CSceneFadeEffect>());
			}
			CSceneFadeEffect fx = target.gameObject.AddComponent<CSceneFadeEffect>();
			fx.mMaskColor = maskColor;
			fx.bIsForward = isForward;

			GameObject effectGo = Instantiate(Resources.Load<GameObject>(sEffectPath));
			SpriteRenderer sr = effectGo.GetComponent<SpriteRenderer>();
			Animator effectAnimator = effectGo.GetComponent<Animator>();
			CSceneFadeBehaviour behaviour = effectAnimator.GetBehaviour<CSceneFadeBehaviour>();

			WaitForSceneFade wait = behaviour.Play(effectAnimator, isForward);
			fx.StartCoroutine(fx.Play(wait, sr));
			return wait;
		}

		private Color mMaskColor;
		private bool bIsForward;
		private float theta;

		#region 动画参数

		//以下参量的空间单位为弧度，时间单位为秒
		private float mInitVelocity = Mathf.PI; //初始速度
		private float mAcceleration = 5 * Mathf.PI;	//恒定加速度
		private float mFrictionalDamping = 0.1f * Mathf.PI; //摩擦阻尼
		private float mCollitionDamping = 0.5f; //碰撞衰减系数
		private float mBounceThreshold = 0.2F * Mathf.PI; //回弹需要的最小速度阈值

		#endregion

		private Material material;

		private void Init()
		{
			Shader shader = Shader.Find("PostEffect/SceneFadeEffect2");
			material = CheckShaderAndCreateMaterial(shader);

			material.SetColor("_MaskColor", mMaskColor);
			material.SetInt("_IsForward", bIsForward ? 1 : 0);
			
		}

		private void OnRenderImage(RenderTexture source, RenderTexture destination)
		{
			if (material != null)
			{
				Graphics.Blit(source, destination, material);
			}
		}

		private IEnumerator Play(WaitForSceneFade wait, SpriteRenderer aniSpriteRenderer)
		{
			////初始化材质等
			//Init();
			////播放动画
			//theta = 0;
			//float velocity = mInitVelocity;
			//bool isDone = false;
			//while(!isDone)
			//{
			//	//利用显示欧拉法预计算加速度
			//	float a0 = mAcceleration - velocity * mFrictionalDamping;
			//	float v0 = velocity;
			//	float v1 = v0 + a0 * Time.deltaTime;
			//	float a1 = mAcceleration - v1 * mFrictionalDamping;
			//	//使用改进欧拉法计算速度
			//	velocity += 0.5f * (a0 + a1) * Time.deltaTime;
			//	//使用梯形法计算位移
			//	theta += 0.5f * (v0 + velocity) * Time.deltaTime;
			//	//检测弹跳
			//	if (theta >= Mathf.PI)
			//	{
			//		theta = Mathf.PI;
			//		velocity = -velocity * mCollitionDamping;
			//		if (Mathf.Abs(velocity) < mBounceThreshold)
			//			isDone = true;
			//	}
			//	//应用theta
			//	material.SetFloat("_Angle", theta);
			//	//下一帧
			//	yield return null;
			//}
			////播放完毕，延迟一会儿再删除脚本
			//wait.isDown = true;
			//yield return new WaitForSeconds(1.0f);
			//Destroy(this);

			yield return null;
			Init();
			while (!wait.IsReady())
			{
				while (!aniSpriteRenderer.sprite)
				{
					yield return null;
				}
				material.SetTexture("_AniTex", aniSpriteRenderer.sprite.texture);
				yield return null;
			}
			yield return new WaitForSeconds(1.0f);
			Destroy(this);
		}

	}
}